<?php
include_once "MySQL_DAO.php";
/**
 * Description of QueryDAO
 *
 * @author Ronicus
 */
class QueryDAO extends MySQL_DAO {

    function chooseRandom($query,$columnName){
        // Perform Query
        $con = $this->connect();
        $resultSet = mysqli_query($con,$query);
        // close connection to db
        $this->close($con);

        // Check result
        // This shows the actual query sent to MySQL, and the error. Useful for debugging.
        if (!$resultSet) {
            $message  = 'Invalid query: ' . mysql_error() . "\n";
            $message .= 'Whole query: ' . $query;
            die($message);
        }

        $numberResults = mysqli_num_rows($resultSet);
        $choice = rand(1,$numberResults);
        // cant find how to pick a single row from a resultset
        // doing a hack
        for($i=0;$i<$choice;$i++){
            $row = mysqli_fetch_assoc($resultSet);
            $item = $row[$columnName];
        }
        mysqli_free_result($resultSet);
        return $item;
    }
}
?>
